package com.xxx.springboot.config;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.eventbus.AsyncEventBus;

/**
 * EventBus配置
 * ① pom.xml引入guava依赖
 * ② 添加 EventBus配置： EventBusConfig 
 * ③ 添加 EventBus处理类：EventBusHandler
 * ④ 使用方式：asyncEventBus.post(username);
 */
@Configuration
public class EventBusConfig {

    /**
     * EventBus初始化（异步）
     */
	@Bean
	public AsyncEventBus initEventBus() {
		// 执行线程池
		ExecutorService executor = new ThreadPoolExecutor(
									10, 
									100,
									60L, 
									TimeUnit.MINUTES,
									new ArrayBlockingQueue<>(10), 
									new ThreadPoolExecutor.DiscardOldestPolicy());
		return new AsyncEventBus(executor);
	}
}
